package com.neu.wms.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.wms.dto.UserDto;
import com.neu.wms.entity.SysUser;
import com.neu.wms.service.UserService;
import com.neu.wms.util.ResultVo;
import com.neu.wms.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：fengSir
 * @date ：Created By 2023-06-20 10:31
 * @description ：TODO
 */
@RestController
@RequestMapping("/users")
@Slf4j
public class UserController {
	@Autowired
	private UserService userService;

	@Autowired
	private PasswordEncoder passwordEncoder;

	//配置了对应的Security拦截了users/login请求，所以这里不需要再使用这个方法了
/*    @PostMapping("login")

    public ResultVo login(@RequestBody UserDto user){
        SysUser sysUser = userService.selectByNameAndPwd(user);
        if (sysUser != null) {
            List<MenuVo> menuVos = userService.selectMenuByName(user.getUserName(), 0);
            return ResultVo.ok("登录成功").data("user",sysUser).data("menuList",menuVos);
        }else {
            return ResultVo.error("用户名或密码错误");
        }

    }*/
	@PostMapping("queryAll")
	public ResultVo queryAll(@RequestBody UserDto userDto) {
		PageHelper.startPage(userDto.getCurrentPage(), userDto.getPageSize(), true);
		List<UserVo> users = userService.selectAll(userDto);
		PageInfo<UserVo> pageInfo = new PageInfo<>(users);
		return ResultVo.ok().data("list", users).data("total", pageInfo.getTotal());
	}

	@GetMapping("checkName")
	public ResultVo checkName(String userName) {
		SysUser sysUser = userService.selectByUsername(userName);
		if (sysUser != null) {
			return ResultVo.ok();
		} else {
			return ResultVo.error();
		}

	}

	@GetMapping("{id}")
	public ResultVo queryById(@PathVariable("id") Long id) {
		SysUser user = userService.selectByPrimaryKey(id);
		log.info("根据id{}得到对象{}", id, user);
		log.info("aaa");
		log.debug("debug");
		log.warn("warn");
		log.error("error");
		return ResultVo.ok().data("user", user);
	}

	@PostMapping("")
	public ResultVo add(@RequestBody SysUser user) {

		//TODO 密码加密 不过我觉得逻辑有点问题
		user.setPassword(passwordEncoder.encode(user.getPassword()));

		int i = userService.insertSelective(user);
		if (i > 0) {
			return ResultVo.ok("添加成功");
		} else {
			return ResultVo.error("添加失败");
		}
	}

	@PutMapping("")
	public ResultVo update(@RequestBody SysUser user) {
		//TODO 密码加密 不过我觉得逻辑有点问题
		user.setPassword(passwordEncoder.encode(user.getPassword()));

		int i = userService.updateByPrimaryKeySelective(user);
		if (i > 0) {
			return ResultVo.ok("添加成功");
		} else {
			return ResultVo.error("添加失败");
		}
	}

	@DeleteMapping("{id}")
	@PreAuthorize("hasAuthority(\"system:user:delete\")")
	//@Secured("ROLE_管理员")
	public ResultVo delete(@PathVariable("id") Long id) {
		int i = userService.deleteByPrimaryKey(id);
		if (i > 0) {
			return ResultVo.ok("删除成功");
		} else {
			return ResultVo.error("删除失败");
		}
	}
}
